App.controller('addUserController', ['$rootScope', 'date_access_service', 'APP_CONSTANT', '$scope', '$state', '$http', '$timeout', 'Utils', '$log', '$modal',
    function ($rootScope, dateAccessService, APP_CONSTANT, $scope, $state, $http, $timeout, Utils, $log, $modal) {
        //初始化数据
        $scope.initData = function () {
            $scope.submitted = false;
            $scope.user = {};
            $scope.user.gender = 1;
        }
        //验证显示
        $scope.validateInput = function (name, type) {
            var input = $scope.formValidate[name];
            return (input.$dirty || $scope.submitted) && input.$error[type];
        };
        //提交add
        $scope.add = function () {

            $scope.submitted = true;
            if ($scope.formValidate.$valid) {
                var params = {
                    name: $scope.user.name,
                    email: $scope.user.email,
                    age: $scope.user.age,
                    gender: $scope.user.gender,
                    birthday: $scope.user.birthday
                };
                console.log("tttt11");
                dateAccessService.addOne('/web/user/addOne', params, function (response) {
                    console.log("wwww");
                    var modalInstance = $modal.open({
                        templateUrl: 'common_after_add',
                        controller: 'commonModelCtrl',
                        resolve: {
                            modelTransferDate: function () {
                                return {optType: 'after_add'};
                            }
                        }
                    });
                    modalInstance.result.then(function (result) {//确定
                        $state.go('app.user.add',{},{reload: true});
                    }, function (reason) {//取消
                        $state.go('app.user.query',{},{reload: true});
                    });
                });
            } else {
                return false;
            }
        };
    }]);

App.controller('queryUserController', ['$scope', 'ngDialog', 'uiGridConstants', '$http', 'date_access_service', function ($scope, ngDialog, uiGridConstants, $http, dateAccessService) {
    //分页配置
    var paginationOptions = {
        pageNumber: 1,
        pageSize: 10,
        sort: null
    };
    //grid配置
    $scope.gridOptions = {
        paginationPageSizes: [25, 50, 75],
        paginationPageSize: 25,
        useExternalPagination: true,
        useExternalSorting: true,
        columnDefs: [
            {name: "id", displayName: "Id", field: "id", visible: true},
            {name: 'name', displayName: "姓名", enableSorting: false},
            {name: 'email', displayName: "电子邮件", enableSorting: false},
            {name: 'age', displayName: "年龄", enableSorting: false},
            {name: 'birthday', displayName: "出生日期", enableSorting: false, cellFilter: 'date:"yyyy-MM-dd"'},
            {name: 'gender', displayName: "性别", enableSorting: false, cellFilter: 'gender_filter'},
            {
                name: 'operation', displayName: "操作", enableSorting: false,
                cellTemplate: '<div><a ui-sref="app.user.view({userId:row.entity.id})" class="text-muted">查看</a>&nbsp;&nbsp;' +
                '<a ui-sref="app.user.update({userId:row.entity.id})" class="text-muted">更新</a>&nbsp;&nbsp;' +
                '<a href="" ng-click="grid.appScope.clickOfDelete(grid,row)" class="text-muted">删除</a></div>'
            }
        ],
        onRegisterApi: function (gridApi) {
            $scope.gridApi = gridApi;
            gridApi.pagination.on.paginationChanged($scope, function (currentPage, pageSize) {
                paginationOptions.pageNumber = currentPage;
                paginationOptions.pageSize = pageSize;
                getPage();
            });
        }
    };
    //删除
    $scope.clickOfDelete = function (grid, row) {
        ngDialog.openConfirm({
                template: 'deleteUserDialog2',
                className: 'ngdialog-theme-default',
                data: {id: row.entity.id},
                scope: $scope
            })
            .then(function (value) {
                var deleteUrl = "/web/user/deleteOne";
                dateAccessService.deleteOne(deleteUrl, value.id, function () {
                    getPage();
                });
            }, function (value) {});
    }
    //查询
    $scope.query = function () {
        getPage();
    };
    //分页
    var getPage = function () {
        var url = "/web/user/queryPage";
        var params = {
            pageNumber: paginationOptions.pageNumber,
            pageSize: paginationOptions.pageSize
        };
        if ($scope.name == null || $scope.name == undefined){
            params.name = $scope.name;
        }
        dateAccessService.getListDate(url, params, function (result) {
            $scope.gridOptions.totalItems = result.total;
            $scope.gridOptions.data = result.entities;
        }, null);
    };

    getPage();

}]);

App.controller('updateUserController', ['$rootScope', 'date_access_service', '$scope', '$state', '$http', '$timeout', 'Utils', '$stateParams','$log', '$modal',
    function ($rootScope, dateAccessService, $scope, $state, $http, $timeout, Utils, $stateParams,$log,$modal) {
        //初始化数据
        $scope.initData = function () {
            var url = "/web/user/queryOne";
            var id = $stateParams.userId;
            $scope.user = {};
            dateAccessService.getOne(url, id, function (data) {
                $scope.user = data;
            });
        }
        //验证显示
        $scope.validateInput = function (name, type) {
            var input = $scope.formValidate[name];
            return (input.$dirty || $scope.submitted) && input.$error[type];
        };
        //更新
        $scope.update = function () {
            $scope.submitted = true;
            if ($scope.formValidate.$valid) {
                var params = {
                    name: $scope.user.name,
                    email: $scope.user.email,
                    age: $scope.user.age,
                    gender: $scope.user.gender,
                    birthday: $scope.user.birthday
                };
                dateAccessService.updateOne('/web/user/updateOne', $stateParams.userId, params, function (response) {
                    var modalInstance = $modal.open({
                        templateUrl: 'common_after_update',
                        controller: 'commonModelCtrl',
                        resolve: {
                            modelTransferDate: function () {
                                return {optType: 'after_update'};
                            }
                        }
                    });
                    modalInstance.result.then(function (result) {//确定
                        $state.go('app.user.query',{reload: true});
                    }, function (reason) {//取消
                    });
                });
            } else {
                return false;
            }
        };

    }]);

App.controller('viewUserController', ['$scope', "$stateParams", 'date_access_service', function ($scope, $stateParams, dateAccessService) {
    //初始化数据
    $scope.initData = function () {
        var url = "/web/user/queryOne";
        var id = $stateParams.userId;
        dateAccessService.getOne(url, id, function (data) {
            $scope.user = data;
        });
    }
}]);

